/*
 * icon.c
 *
 *  Created on: Jan 3, 2012
 *      Author: ian
 */

#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "icon.h"
#include "lcd.h"

extern const prog_uint8_t bmp_icons[];


int8_t _dispIcons = 0;
int8_t wIcons = 0;
uint8_t _forceUpdateMsk = 0;
eLightsIcon wLightMode = LIGHTS_OFF;
eLightsIcon _dispLightMode = LIGHTS_OFF;

void drawIcon( eIcon icon );
void blankIcon( eIcon icon );

void ICON_clear()
{
	//next call to ICON_updatewill draw all
	_dispIcons = 0;
}

void ICON_update()
{
	uint8_t chng = (_dispIcons ^ wIcons);	// returns changed icons
	uint8_t i;

	chng |= _forceUpdateMsk;
	_forceUpdateMsk = 0;

	for( i = 0; i < 8; i++ ) {
		if( chng & (1<<i) ) {
			if( wIcons & (1<<i) ) {
				drawIcon( i );
			}
			else {
				blankIcon( i );
			}
		}
	}

	if( _dispLightMode != wLightMode ) {
		switch( wLightMode ) {
		case LIGHTS_OFF:
		case LIGHTS_SIDE:
			blankIcon( ICON_DIP );
			break;
		case LIGHTS_DIP:
			drawIcon( ICON_DIP );
			break;
		case LIGHTS_MAIN:
			drawIcon( ICON_MAIN );
			break;
		}
	}

	_dispIcons = wIcons;
	_dispLightMode = wLightMode;
}


void ICON_lights( eLightsIcon lights )
{
	wLightMode = lights;
}

void ICON_show( eIcon icon, uint8_t val )
{
	if( val ) {
		wIcons |= (1<<icon);
	}
	else {
		wIcons &= ~(1<<icon);
	}
}

void ICON_toggle( eIcon icon )
{
	wIcons = ( wIcons & (1<<icon) ) ? (wIcons & ~(1<<icon)) : (wIcons | (1<<icon));
}

void ICON_nextUpdateAll( void )
{
	_forceUpdateMsk = 0xff;
	_dispLightMode = 0xff;
}

/* privates */

void drawIcon( eIcon icon )
{
	/*
	 * Fog = 0,0,26,21
	 * Warning = 26,0,23,21
	 * Slippery = 49,0,21,21
	 * Battery = 70,0,26,21
	 * Doors = 96,0,21,21
	 * LH = 117,0,15,21
	 * Dip 132,0,26,21
	 * Main = 158,0,26,21
	 * RH = 184,0,16,21
	 * OilP = 200,0,27,21
	 */
	switch( icon ) {
	case ICON_LH:
		LCD_BitmapWindowTint(39, 1, 117, 0, 15, 21, bmp_icons, 0b010 );
		break;
	case ICON_RH:
		LCD_BitmapWindowTint(113, 1, 184, 0, 16, 21, bmp_icons, 0b010 );
		break;
	case ICON_WARNING:
		LCD_BitmapWindowTint(28, 154, 26, 0, 23, 21, bmp_icons, 0b110 );
		break;
	case ICON_CHARGE:
		LCD_BitmapWindowTint(81, 154, 70, 0, 26, 21, bmp_icons, 0b100 );
		break;
	case ICON_DOOR:
		LCD_BitmapWindowTint(111, 154, 96, 0, 21, 21, bmp_icons, 0b100 );
		break;
	case ICON_FOG:
		LCD_BitmapWindowTint(0, 154, 0, 0, 26, 21, bmp_icons, 0b010 );
		break;
	case ICON_OILP:
		LCD_BitmapWindowTint(52, 154, 200, 0, 27, 21, bmp_icons, 0b100 );
		break;
	case ICON_DIP:
		LCD_BitmapWindowTint(70, 1, 132, 0, 26, 21, bmp_icons, 0b010 );
		break;
	case ICON_MAIN:
		LCD_BitmapWindowTint(70, 1, 158, 0, 26, 21, bmp_icons, 0b001 );
		break;
	}
}

void blankIcon( eIcon icon )
{
	switch( icon ) {
	case ICON_LH:
		LCD_Box(39, 1, 55, 21, black );
		break;
	case ICON_RH:
		LCD_Box( 113, 1, 130, 21, black );
		break;
	case ICON_WARNING:
		LCD_Box( 28, 154, 51, 174, black );
		break;
	case ICON_CHARGE:
		LCD_Box( 81, 154, 106, 174, black );
		break;
	case ICON_DOOR:
		LCD_Box( 111, 154, 131, 174, black );
		break;
	case ICON_FOG:
		LCD_Box( 0, 154, 25, 174, black );
		break;
	case ICON_OILP:
		LCD_Box( 52, 154, 79, 174, black );
		break;
	case ICON_DIP:
	case ICON_MAIN:
		LCD_Box( 70, 1, 96, 21, black );
		break;
	}
}


